//2007/10/12: Eric Kao was created.
//有關日期

/**
 * 取後系統時間的日期(不包含時分秒)
 */
function getSystemDate()
{
	var systemDate = new Date();
	
	return new Date( systemDate.getFullYear(),　systemDate.getMonth(),　systemDate.getDate() );
}

/**
 * 和系統日期比較( 不比較時分秒 )
 * 1. source > 系統時間: return 1
 * 2. source = 系統時間: return 0
 * 3. source < 系統時間: return -1
 */
function compareDateBySystemDate( source )
{
	return compareDate( source, getSystemDate() );
}

/**
 * 比較兩個日期大小
 * 1. source > comparator: return 1
 * 2. source = comparator: return 0
 * 3. source < comparator: return -1
 */
function compareDate( source, comparator )
{
	var sourceTime = source.getTime();
	var comparatorTime = source.getTime();
	
	if( sourceTime < comparator )
	{
		return -1;
	}
	else if( sourceTime > comparator )
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

/**
 * 系統時間是否落在 block1 和 block2 之間
 * true: 系統時間在 block1 and block2, false 則不是
 */
function blockDateBySystemDate( block1, block2 )
{
	return blockDate( getSystemDate(), block1, block2 );
}

/**
 * 傳入 source 的日期物件, 是否在 block1 日期和 block2 日期 之間
 * true: 系統時間在 block1 and block2, false 則不是
 */
function blockDate( source, block1, block2 )
{
	if( block1 == null && block2 == null )
	{
		return false;
	}
	else if( block1 == null )
	{
		return compareDate( source, block2 ) <= 0;
			
	}
	else if( block2 == null )
	{
		return compareDate( source, block1 ) >= 0;
	}
	else
	{
		return compareDate( source, block1 ) >= 0 && compareDate( source, block2 ) <= 0;
	}
}

/**
 * trans String to Date
 * param: ex, 20070901 or 2007/09/01
 * return: Date 物件
 */
function transString2Date( source )
{
	if( source == null )
	{
		alert( "Error method transString2Date() in e885DateUtil.js: source is null" );
		
		return false;
	} 

	if( !( source.length == 8 || source.length == 10 ) )
	{
		alert( "日期格式錯誤" );
		
		return false;
	}
	
	var year;
	var month;
	var day;
	
	if( source.length == 8 )
	{
		year = source.substring( 0, 4 );
		month = source.substring( 4, 6 );
		day = source.substring( 6, 8 );				
	}
	
	if( source.length == 10 )
	{
		year = source.substring( 0, 4 );
		month = source.substring( 5, 7 );
		day = source.substring( 8, 10 );
	}
	
	return new Date( year, ( month - 1 ), day );
}
